//
//  Data.m
//
//  Created by lianchuang  on 14-11-24
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "SquareModelData.h"


NSString *const kDataDisplayorder = @"displayorder";
NSString *const kDataSubject = @"subject";
NSString *const kDataTypeid = @"typeid";
NSString *const kDataDigest = @"digest";
NSString *const kDataClosed = @"closed";
NSString *const kDataAuthor = @"author";
NSString *const kDataLastpost = @"lastpost";
NSString *const kDataAuthorid = @"authorid";
NSString *const kDataHighlight = @"highlight";
NSString *const kDataPic = @"pic";
NSString *const kDataGid = @"gid";
NSString *const kDataGood = @"good";
NSString *const kDataSummary = @"summary";
NSString *const kDataDateline = @"dateline";
NSString *const kDataReplies = @"replies";
NSString *const kDataTid = @"tid";
NSString *const kDataCollection = @"collection";
NSString *const kDataViews = @"views";


@interface SquareModelData ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SquareModelData

@synthesize displayorder = _displayorder;
@synthesize subject = _subject;
@synthesize typeid = _typeid;
@synthesize digest = _digest;
@synthesize closed = _closed;
@synthesize author = _author;
@synthesize lastpost = _lastpost;
@synthesize authorid = _authorid;
@synthesize highlight = _highlight;
@synthesize pic = _pic;
@synthesize gid = _gid;
@synthesize good = _good;
@synthesize summary = _summary;
@synthesize dateline = _dateline;
@synthesize replies = _replies;
@synthesize tid = _tid;
@synthesize collection = _collection;
@synthesize views = _views;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.displayorder = [self objectOrNilForKey:kDataDisplayorder fromDictionary:dict];
            self.subject = [self objectOrNilForKey:kDataSubject fromDictionary:dict];
            self.typeid = [self objectOrNilForKey:kDataTypeid fromDictionary:dict];
            self.digest = [self objectOrNilForKey:kDataDigest fromDictionary:dict];
            self.closed = [self objectOrNilForKey:kDataClosed fromDictionary:dict];
            self.author = [self objectOrNilForKey:kDataAuthor fromDictionary:dict];
            self.lastpost = [self objectOrNilForKey:kDataLastpost fromDictionary:dict];
            self.authorid = [self objectOrNilForKey:kDataAuthorid fromDictionary:dict];
            self.highlight = [self objectOrNilForKey:kDataHighlight fromDictionary:dict];
            self.pic = [self objectOrNilForKey:kDataPic fromDictionary:dict];
            self.gid = [self objectOrNilForKey:kDataGid fromDictionary:dict];
            self.good = [self objectOrNilForKey:kDataGood fromDictionary:dict];
            self.summary = [self objectOrNilForKey:kDataSummary fromDictionary:dict];
            self.dateline = [self objectOrNilForKey:kDataDateline fromDictionary:dict];
            self.replies = [self objectOrNilForKey:kDataReplies fromDictionary:dict];
            self.tid = [self objectOrNilForKey:kDataTid fromDictionary:dict];
            self.collection = [[self objectOrNilForKey:kDataCollection fromDictionary:dict] doubleValue];
            self.views = [self objectOrNilForKey:kDataViews fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.displayorder forKey:kDataDisplayorder];
    [mutableDict setValue:self.subject forKey:kDataSubject];
    [mutableDict setValue:self.typeid forKey:kDataTypeid];
    [mutableDict setValue:self.digest forKey:kDataDigest];
    [mutableDict setValue:self.closed forKey:kDataClosed];
    [mutableDict setValue:self.author forKey:kDataAuthor];
    [mutableDict setValue:self.lastpost forKey:kDataLastpost];
    [mutableDict setValue:self.authorid forKey:kDataAuthorid];
    [mutableDict setValue:self.highlight forKey:kDataHighlight];
    NSMutableArray *tempArrayForPic = [NSMutableArray array];
    for (NSObject *subArrayObject in self.pic) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForPic addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForPic addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForPic] forKey:kDataPic];
    [mutableDict setValue:self.gid forKey:kDataGid];
    [mutableDict setValue:self.good forKey:kDataGood];
    [mutableDict setValue:self.summary forKey:kDataSummary];
    [mutableDict setValue:self.dateline forKey:kDataDateline];
    [mutableDict setValue:self.replies forKey:kDataReplies];
    [mutableDict setValue:self.tid forKey:kDataTid];
    [mutableDict setValue:[NSNumber numberWithDouble:self.collection] forKey:kDataCollection];
    [mutableDict setValue:self.views forKey:kDataViews];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.displayorder = [aDecoder decodeObjectForKey:kDataDisplayorder];
    self.subject = [aDecoder decodeObjectForKey:kDataSubject];
    self.typeid = [aDecoder decodeObjectForKey:kDataTypeid];
    self.digest = [aDecoder decodeObjectForKey:kDataDigest];
    self.closed = [aDecoder decodeObjectForKey:kDataClosed];
    self.author = [aDecoder decodeObjectForKey:kDataAuthor];
    self.lastpost = [aDecoder decodeObjectForKey:kDataLastpost];
    self.authorid = [aDecoder decodeObjectForKey:kDataAuthorid];
    self.highlight = [aDecoder decodeObjectForKey:kDataHighlight];
    self.pic = [aDecoder decodeObjectForKey:kDataPic];
    self.gid = [aDecoder decodeObjectForKey:kDataGid];
    self.good = [aDecoder decodeObjectForKey:kDataGood];
    self.summary = [aDecoder decodeObjectForKey:kDataSummary];
    self.dateline = [aDecoder decodeObjectForKey:kDataDateline];
    self.replies = [aDecoder decodeObjectForKey:kDataReplies];
    self.tid = [aDecoder decodeObjectForKey:kDataTid];
    self.collection = [aDecoder decodeDoubleForKey:kDataCollection];
    self.views = [aDecoder decodeObjectForKey:kDataViews];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_displayorder forKey:kDataDisplayorder];
    [aCoder encodeObject:_subject forKey:kDataSubject];
    [aCoder encodeObject:_typeid forKey:kDataTypeid];
    [aCoder encodeObject:_digest forKey:kDataDigest];
    [aCoder encodeObject:_closed forKey:kDataClosed];
    [aCoder encodeObject:_author forKey:kDataAuthor];
    [aCoder encodeObject:_lastpost forKey:kDataLastpost];
    [aCoder encodeObject:_authorid forKey:kDataAuthorid];
    [aCoder encodeObject:_highlight forKey:kDataHighlight];
    [aCoder encodeObject:_pic forKey:kDataPic];
    [aCoder encodeObject:_gid forKey:kDataGid];
    [aCoder encodeObject:_good forKey:kDataGood];
    [aCoder encodeObject:_summary forKey:kDataSummary];
    [aCoder encodeObject:_dateline forKey:kDataDateline];
    [aCoder encodeObject:_replies forKey:kDataReplies];
    [aCoder encodeObject:_tid forKey:kDataTid];
    [aCoder encodeDouble:_collection forKey:kDataCollection];
    [aCoder encodeObject:_views forKey:kDataViews];
}

- (id)copyWithZone:(NSZone *)zone
{
    SquareModelData *copy = [[SquareModelData alloc] init];
    
    if (copy) {

        copy.displayorder = [self.displayorder copyWithZone:zone];
        copy.subject = [self.subject copyWithZone:zone];
        copy.typeid = [self.typeid copyWithZone:zone];
        copy.digest = [self.digest copyWithZone:zone];
        copy.closed = [self.closed copyWithZone:zone];
        copy.author = [self.author copyWithZone:zone];
        copy.lastpost = [self.lastpost copyWithZone:zone];
        copy.authorid = [self.authorid copyWithZone:zone];
        copy.highlight = [self.highlight copyWithZone:zone];
        copy.pic = [self.pic copyWithZone:zone];
        copy.gid = [self.gid copyWithZone:zone];
        copy.good = [self.good copyWithZone:zone];
        copy.summary = [self.summary copyWithZone:zone];
        copy.dateline = [self.dateline copyWithZone:zone];
        copy.replies = [self.replies copyWithZone:zone];
        copy.tid = [self.tid copyWithZone:zone];
        copy.collection = self.collection;
        copy.views = [self.views copyWithZone:zone];
    }
    
    return copy;
}


@end
